package com.itheima.test;

import java.io.*;
import java.util.ArrayList;
import java.util.TreeSet;

public class HeroSort {
    public static void main(String[] args) throws Exception{
        TreeSet<Hero> heroes = new TreeSet<>((o1, o2) -> Double.compare(o2.getAdapt(),o1.getAdapt()));
        String path = "D:\\source_code\\workspace\\stream-file-app\\src\\com\\itheima\\test\\hero.txt";
        BufferedReader br = new BufferedReader(new FileReader(path));
        String line = br.readLine();
        System.out.println(line);
        while((line = br.readLine()) != null){
            String[] split = line.split(",+");
            Hero hero = new Hero(split[0], Integer.parseInt(split[1]), Integer.parseInt(split[2]), Integer.parseInt(split[3]));
            hero.computingAdapt();
            heroes.add(hero);
        }
        heroes.forEach(System.out::println);

        BufferedWriter bw = new BufferedWriter(new FileWriter(path));
        bw.write("姓名 生存 攻击 效果");
        bw.newLine();
        heroes.forEach(s->{
            try {
                bw.write(s.getName() + "," + s.getHP() + "," + s.getAD() + "," + s.getAP());
                bw.newLine();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        });
        bw.close();
        br.close();
    }
}
